//
//  ST_DailyMomentImageContainer.swift
//  btvideo
//
//  Created by mac on 2020/7/21.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import PINRemoteImage

class ST_DailyMomentImageContainer: UIView {
    
    fileprivate func layoutChild() {
        
        self.subviews.forEach { (v) in
            v.removeFromSuperview()
            if let btn = v as? DailyMomentImageControl{
                btns.update(with: btn)
            }
        }
        
        switch imagesArray.count {
        case 4:
            
            let btn0 = getItem()
            let btn1 = getItem()
            let btn2 = getItem()
            let btn3 = getItem()
            
            self.addSubview(btn0)
            self.addSubview(btn1)
            self.addSubview(btn2)
            self.addSubview(btn3)
            
            btn0.snp.makeConstraints { (make) in
                make.left.top.equalToSuperview()
                make.right.equalTo(btn1.snp.left).offset(-5)
            }
            
            btn1.snp.makeConstraints { (make) in
                make.right.top.equalToSuperview()
                make.width.height.equalTo(btn0)
            }
            
            btn2.snp.makeConstraints { (make) in
                make.left.bottom.equalToSuperview()
                make.width.height.equalTo(btn0)
                make.top.equalTo(btn0.snp.bottom).offset(5)
            }
            
            btn3.snp.makeConstraints { (make) in
                make.right.bottom.equalToSuperview()
                make.width.height.equalTo(btn0)
            }
            break
        case 3:
            let btn0 = getItem()
            let btn1 = getItem()
            let btn2 = getItem()
            
            self.addSubview(btn0)
            self.addSubview(btn1)
            self.addSubview(btn2)
            
            btn0.snp.makeConstraints { (make) in
                make.left.top.bottom.equalToSuperview()
            }
            
            btn1.snp.makeConstraints { (make) in
                make.height.width.equalTo(btn0)
                make.left.equalTo(btn0.snp.right).offset(5)
                make.top.equalTo(btn0)
            }
            
            btn2.snp.makeConstraints { (make) in
                make.height.width.equalTo(btn0)
                make.right.equalToSuperview()
                make.left.equalTo(btn1.snp.right).offset(5)
                make.top.equalTo(btn0)
            }
            
            break
        case 2:
            let btn0 = getItem()
            let btn1 = getItem()
            
            self.addSubview(btn0)
            self.addSubview(btn1)
            
            btn0.snp.makeConstraints { (make) in
                make.left.top.bottom.equalToSuperview()
            }
            
            btn1.snp.makeConstraints { (make) in
                make.height.width.equalTo(btn0)
                make.right.equalToSuperview()
                make.left.equalTo(btn0.snp.right).offset(5)
                make.top.equalTo(btn0)
            }
            
            break
        case 1:
            
            let btn0 = getItem()
            
            self.addSubview(btn0)
            
            btn0.snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
            }
            break
        default:
            break
        }
    }
    
    
    fileprivate func bind_model(){
        
        for (idx,v) in self.subviews.enumerated(){
            
            if let btn = v as? DailyMomentImageControl{
                btn.child = imagesArray[safe:idx]
            }
        }
        
    }
    
    
    fileprivate func getItem()->DailyMomentImageControl{
        
        if btns.isEmpty{
            let btn = DailyMomentImageControl()
            btn.clipsToBounds = true
            btn.cornerRadius = 15
            btn.addTarget(self, action: #selector(preview_handle(sender:)), for: UIControl.Event.touchUpInside)
            return btn
        }else{
            return btns.removeFirst()
        }
    }
    
    
    @objc fileprivate func preview_handle(sender:DailyMomentImageControl){
        
        guard let model = sender.child else{
            return
        }
        
        
        if model.videoUrl.count > 2{
            
            let playCtr = ST_BTPlayerViewController()
            
            playCtr.show_delete_action = false
            
            playCtr.type = .networkUrl
            
            playCtr.videoUrl = model.videoUrl
            
            GlobalPresent(toController: playCtr)
            
        }else{
            
            let index = imagesArray.firstIndex(where: {( $0 == model )})?.double.int
            
            guard let idx = index else{
                return
            }
            self.previewPicures(index: idx)
            
        }
        
        
        
        
        
    }
    
    fileprivate func previewPicures(index:Int){
        
       var array = [ST_MWPhoto]()
        
       for model in imagesArray{
        
        guard let url = model.content.url,let media = ST_MWPhoto.init(url:url ) else{
            
           array.append(ST_MWPhoto())
               continue
           }
           array.append(media)
       }
       
       guard let browser = ST_MWPhotoBrowser.init(photos: array) else{
           return
       }
       browser.displayActionButton = false;
        browser.setCurrentPhotoIndex(UInt(index))
       
       let nav = UINavigationController.init(rootViewController: browser)
       
       GlobalPresent(toController: nav)
        
    }
    
    
//    MARK: - Properties
    fileprivate var btns = Set<DailyMomentImageControl>()
    
    fileprivate var currentNumbers:Int?
    
    var imagesArray = [DailyMomentMedia](){
        didSet{
            
            if self.subviews.count != imagesArray.count{
             
                layoutChild()
                
            }else{
                print("相同-----22")
            }
            bind_model()
        }
    }
}



fileprivate class DailyMomentImageControl: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(sign_image_view)
        sign_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(46)
            make.center.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    fileprivate lazy var cover_image_view:PINAnimatedImageView = {
         let control = PINAnimatedImageView()
          control.contentMode = .scaleAspectFill
          return control
      }()
    
    fileprivate lazy var sign_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("daily_video_icon"))
        control.isHidden = true
        return control
    }()
    
    
    weak var child:DailyMomentMedia?{
        didSet{
            guard let m = child else{
                return
            }
            
            cover_image_view.pin_setImage(from: m.content.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image()) { (result) in
                
                /*
                if let err = result.error{
                    print("-----WebP----",err,"----URL:",result.response?.url)
                }*/
            }
            
            sign_image_view.isHidden = m.videoUrl.count < 2
            
            
        }
    }
    
}
